import { getToken, setToken, removeToken, setStorage, removeStorage } from '../../utils/auth'
import { login } from '../../api/login.js'
import Config from '../../config'
import $router from '../../router/index'

let user = {
    state: {
        routers: [],
        addRouters: [],
        isLogin: true,//是否登录
        hasToken: getToken(),//是否有token
        loadMenus: false,
        footerTxt: Config.footerTxt,
        caseNumber: Config.caseNumber,
    },
    mutations: {
        SET_ROUTERS: (state, routers) => {
            state.addRouters = $router.options.routes
            state.routers = [state.addRouters[0], ...routers]
        },
        SET_LOGIN(state, bool) {
            state.isLogin = bool
        },
        SET_TOKEN(state, { token, rememberMe }) {
            setToken(token, rememberMe)
            state.hasToken = token
        },
        REMOVE_TOKEN(state, token) {
            removeToken()
            state.hasToken = token
        },
        SET_LOAD_MENUS: (state, loadMenus) => {
            state.loadMenus = loadMenus
        }
    },
    actions: {
        GenerateRoutes({ commit }, asyncRouter) {
            commit('SET_ROUTERS', asyncRouter)
        },
        updateIsLogin(context, state) {
            context.commit('SET_LOGIN', state)
        },
        login({ commit }, { username, password, rememberMe }) {
            //调用登录接口
            return new Promise<void>((resolve, reject) => {
                login({ username, password }).then((res) => {
                    //存token
                    //setToken(res.data.token,)
                    commit('SET_TOKEN', { token: res.token, rememberMe: rememberMe });
                    // 第一次加载菜单时用到， 具体见 src 目录下的 permission.js
                    commit('SET_LOAD_MENUS', true)
                    let routes = $router.options.routes
                    setStorage("currentMenu", routes[0].path);
                    setStorage("sideMenu", JSON.stringify(routes[0].children));
                    resolve()
                }).catch(error => {
                    console.log(error);
                    reject(error)
                })
            })

        },
        // 登出
        LogOut({ commit }) {
            return new Promise<void>((resolve, reject) => {
                commit('REMOVE_TOKEN', '')
                removeStorage('currentMenu')
                removeStorage('currentPath')
                removeStorage('sideMenu')
                resolve()
            })
        },
        updateLoadMenus({ commit }) {
            return new Promise((resolve, reject) => {
                commit('SET_LOAD_MENUS', false)
            })
        }
    },
    /*     getters: {
            getIsLogin: state => state.isLogin,
            getToken: state => state.hasToken,
            getMenuData: state => state.routers,
            loadMenus: state => state.loadMenus,
            footerTxt: state => state.footerTxt,
            caseNumber: state => state.caseNumber,
        } */
};
export default user